package com.deng.mapper;

import com.deng.model.Friend;
import com.deng.model.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author 邓小熙
 * @Date 2022/5/17 9:47
 * @Version 1.0
 */
@Mapper
@Component
public interface FriendMapper {
    /**
     * 个人页面的好友列表 开放权限
     * @param userId
     * @param PageNum
     * @param pageSize
     * @return
     */
    @Select("SELECT u.* FROM social_blog.users u,social_blog.friends f " +
            "WHERE f.friend_id = u.id AND f.user_id=#{userId} LIMIT #{PageNum},#{pageSize}")
    List<User> findFriendsWithUserId(@Param("userId") int userId, @Param("PageNum")int PageNum, @Param("pageSize")int pageSize);
    /**
     * 检查是否关注该博主
     * @param userId
     * @param friendId
     * @return
     */
    @Select("SELECT * FROM social_blog.friends WHERE user_id=#{userId} AND friend_id=#{friendId}")
    Friend findFriendById(@Param("userId") int userId, @Param("friendId") int friendId);
    /**
     * 关注博主
     * @param userId
     * @param friendId
     * @return
     */
    @Insert("INSERT INTO social_blog.friends(user_id,friend_id) VALUES(#{userId},#{friendId})")
    int insertFriendRelation(@Param("userId") int userId, @Param("friendId") int friendId);
    /**
     * 取消关注
     * @param userId
     * @param friendId
     * @return
     */
    @Delete("DELETE FROM social_blog.friends WHERE user_id=#{userId} and friend_id=#{friendId}")
    int deleteFriendRelation(@Param("userId") int userId, @Param("friendId") int friendId);
    /**
     * 查看关注列表
     * @param userId
     * @param page
     * @param limit
     * @return
     */
    @Select("SELECT * FROM social_blog.friends WHERE user_id=#{userId} LIMIT #{page},#{limit}")
    List<Friend> findAllFriend(@Param("userId") int userId ,@Param("page")int page ,@Param("limit") int limit);
    /**
     * 通过用户名返回用户的数量
     * @param userId
     * @return
     */
    @Select("SELECT COUNT(*) FROM social_blog.friends WHERE user_id=#{userId}")
    int findCountOfFriend(@Param("userId") int userId);
}
